package com.py.credit.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.service.PromiseBookApprovalService;
import com.py.credit.utils.FileUtil;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.common.Page;
import com.py.framework.core.exception.ServiceException;

@RestController
@RequestMapping(value = "/xycn")
public class XycnController {
	@Autowired
	private PromiseBookApprovalService promiseBookApprovalService;
	
	@IgnoreAuthority
	@RequestMapping(value="/selectXycn.json")
	public RestServiceResult<Object> selectXycn(String condition){
		RestServiceResult<Object> r = new RestServiceResult<>();
		Page page  = new Page();
		Map<String,Object> map = JSON.parseObject(condition);
		String name = null;
		String startTime = null;
		String endTime = null;
		String type = null;
		if(map.size()>0) {
			if(map.get("name") != null) {
				name =	String.valueOf(map.get("name"));
			}
			if(map.get("startTime") != null) {
				startTime =	String.valueOf(map.get("startTime"));
			}
			if(map.get("endTime") != null) {
				endTime =	String.valueOf(map.get("endTime"));
			}
			if(map.get("type") != null) {
				type =	String.valueOf(map.get("type"));
			}
		}
		
		try {
			page.setLimit(Integer.parseInt(String.valueOf(map.get("limit"))));
			
			page.setPage(Integer.parseInt(String.valueOf(map.get("page"))) /Integer.parseInt(String.valueOf(map.get("limit"))) + 1);
			
			r.setData(promiseBookApprovalService.selectXycnForPage(name,startTime,endTime,type,page));
            r.setCount(page.getTotalCount());
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("查询信用承诺失败");
		}
		return r;
	}
	
	@IgnoreAuthority
	@RequestMapping(value = "/downFileUrl.json")
	public void downFileUrl(@RequestParam(value = "id", required = true,defaultValue = "") String id,
			HttpServletResponse response, HttpServletRequest request) {
		
		String path = promiseBookApprovalService.promiseBookPreview(Long.valueOf(id));
        
        if(!new File(path).exists()) {
        	throw new ServiceException("文件不存在，或者已经被移除！");
        }
		FileUtil.downloadFile(path, response);
	}
	
	 private byte[] toByteArray(InputStream in) throws IOException {

			ByteArrayOutputStream out = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024 * 4];
			int n = 0;
			while ((n = in.read(buffer)) != -1) {
				out.write(buffer, 0, n);
			}
			return out.toByteArray();
		}
}
